const db = uniCloud.database();
const { guid } = require('../../common/utils');
module.exports = async function(options = {}) {
	const { characterId, uid, name, chapterDesc, logo } = options;
	const token = this.getUniIdToken();
	if (!token || !characterId || !name) {
		throw new Error('非法操作');
	}
	// 获取没有重复的chapter
	async function getChapter() {
		const chapter = `CUSTOM-${guid(8)}`;
		const find = await db.collection('a-dz-chapter-list-custom').where({ chapter }).get({ getOne: true });
		if (find.data.length) {
			return getId();
		}
		return chapter;
	}
	// 获取没有重复的category
	async function getCategory() {
		const category = `CUSTOM-C${guid(8)}`;
		const find = await db.collection('a-dz-category-list-custom').where({ category }).get({ getOne: true });
		if (find.data.length) {
			return getCategory();
		}
		return category;
	}
	// 获取没有重复的课程id
	async function getCourseId() {
		const res = await db.collection('a-dz-course-list-custom').orderBy("courseId","desc").limit(1).get();
		const customId = 8811234567890131;
		if (res.data.length) {
			const item = res.data[0];
			return item.courseId + 888;
		} else {
			return customId;
		}
	}
	// 获取当前用户的自定义课程内容（最后一条记录）
	async function getUserLastCustomCourse() {
		const res = await db.collection('a-dz-course-list-custom').where({ uid, characterId }).orderBy("courseId","desc").limit(1).get();
		if (res.data.length) {
			const item = res.data[0];
			return item;
		} else {
			return {
				courseOrder: 1
			}
		}
	}
	const [characterList, chapter, category, courseId, lastCourse] = await Promise.all([
		db.collection('a-dz-character-list').where({ characterId }).get(),
		getChapter(),
		getCategory(),
		getCourseId(),
		getUserLastCustomCourse()
	])
	if (characterList.data.length) {
		const character = characterList.data[0];
		if (character.type != 'TEACHER') {
			throw new Error('非法操作');
		}
		const createTime = Date.now();
		// 获取组标识
		const courseGroup = `G-${guid(6)}${Date.now()}`;
		// 获取课程顺序
		const courseOrder = lastCourse.courseOrder;
		await Promise.all([
			db.collection('a-dz-chapter-list-custom').add({
				chapter,
				characterId,
				uid,
				name,
				chapterDesc,
				logo,
				createTime
			}),
			db.collection('a-dz-category-list-custom').add({
				chapter,
				characterId,
				uid,
				chapter,
				category,
				name: '默认星球',
				logo: 'https://mp-d35d9931-7118-4dad-8d19-5a58a08d06cd.cdn.bspapp.com/cloudstorage/a6a14b79-194e-4ecb-bb77-f94ea936c018.png'
			}),
			db.collection('a-dz-course-list-custom').add({
				chapter,
				characterId,
				uid,
				chapter,
				category,
				courseId,
				icon: '<svg id="图层_1" data-name="图层 1" xmlns="http://www.w3.org/2000/svg" width="52" height="52" viewBox="0 0 52 52"><defs><style>.icon-1{fill:#9999fd;}</style></defs><path class="icon-1" d="M12,44.83h-.66c-2.24-.07-3-.43-3.44-1-1.44-1.68-.66-13.17,2.81-16.74h0c2.37-2.43,6.87-6.92,11.64-11.66l3.1-3.09h0l1.72-1.71c5-5,11.21-4.07,14.73-.55s4.47,9.72-.55,14.73l-1.72,1.72h0L37,29.14c-5,5-9.66,9.63-12.13,12.14h0C22,44.22,15.13,44.83,12,44.83Zm-1.75-3.08c2,.34,8.17-.08,11.3-1.7L12,30.5c-1.62,3.19-2,9.34-1.77,11.25Zm9.67-7.55,3.9,3.91c2.63-2.66,6.76-6.77,11.09-11.09l1.53-1.53-3.9-3.9Zm-6-6,3.92,3.92L30.41,19.47,26.5,15.55l-2.05,2c-4.13,4.15-8.06,8.07-10.57,10.61Zm14.7-14.77,10,10,.65-.66c3.79-3.79,2.94-8.1.55-10.49s-6.7-3.24-10.49.55Z"></path></svg>',
				courseText: [],
				language: 'CN',
				name: '默认课程',
				courseOrder,
				courseType: 'COPY',
				courseGroup,
				courseGroupName: '默认分组',
				subscriptionType: 'PRO'
			})
		])
		return {
			code: 0,
			msg: '创建成功'
		}
	} else {
		throw new Error('非法操作');
	}
}